package bb.lanxing.common.daemon;

import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.IOException;

public class Daemon {
    private static final String BIN_DIR_NAME = "bin";
    private static final String DAEMON_BIN_NAME = "daemon";
    public static final int INTERVAL_ONE_HOUR = 3600;
    public static final int INTERVAL_ONE_MINUTE = 60;
    private static final String TAG = "Daemon";

    public static void start(Context context, Class<?> cls, int i, int i2) {
        StringBuilder sb = new StringBuilder();
        sb.append(context.getDir("bin", 0).getAbsolutePath() + File.separator + DAEMON_BIN_NAME);
        sb.append(" -p ");
        sb.append(context.getPackageName());
        sb.append(" -s ");
        sb.append(cls.getName());
        sb.append(" -t ");
        sb.append(i);
        sb.append(" -i ");
        sb.append(i2);
        try {
            Log.d(TAG, "start daemon exec: " + sb.toString());
            Runtime.getRuntime().exec(sb.toString()).waitFor();
        } catch (IOException | InterruptedException e) {
            Log.e(TAG, "start daemon error: " + e);
        }
    }

    public static void run(final Context context, final Class<?> cls, final int i, final int i2) {
        new Thread(() -> {
            Command.install(context, "bin", Daemon.DAEMON_BIN_NAME);
            Daemon.start(context, cls, i, i2);
        }).start();
    }
}
